class Solution
{
public:
    vector<int> filterRestaurants(vector<vector<int>> &restaurants, int veganFriendly, int maxPrice, int maxDistance)
    {
        vector<vector<int>> filtered;
        for (auto &r : restaurants)
        {
            if (veganFriendly && !r[2])
            {
                continue;
            }
            if (r[3] > maxPrice)
            {
                continue;
            }
            if (r[4] > maxDistance)
            {
                continue;
            }
            filtered.push_back(r);
        }
        sort(filtered.begin(), filtered.end(), [](auto &r1, auto &r2)
             {
            if(r1[1] == r2[1])
            {
                return r1[0] > r2[0];
            }
            return r1[1] > r2[1]; });
        vector<int> result;
        for (auto &r : filtered)
        {
            result.push_back(r[0]);
        }
        return result;
    }
};